import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:speed_random/app_styles.dart';
import 'package:speed_random/common/common_fuc.dart';
import 'package:speed_random/global_data.dart';
import 'package:speed_random/pages/home/rand_com/com_results/item_results_color.dart';
import 'package:speed_random/pages/home/rand_com/com_results/item_results_com.dart';
import 'package:speed_random/pages/home/rand_com/com_results/item_results_roller.dart';
import 'package:speed_random/pages/home/rand_com/com_results/item_results_spin_wheel.dart';
import 'package:speed_random/pages/home/rand_com/com_results/logic.dart';
import 'package:speed_random/pages/home/rand_com/com_results/state.dart';
import 'package:share_plus/share_plus.dart';

class ItemResults extends StatelessWidget {
  final ComResultsLogic logic;
  final ComResultsState state;
  final Map<String, dynamic> numbers;
  final Color textColor;
  final Color bgColor;
  final bool isRandColor;

  const ItemResults({
    Key? key,
    required this.logic,
    required this.state,
    required this.numbers,
    this.textColor = AppStyles.colorResultsItemText,
    this.bgColor = AppStyles.colorResultsItemBg,
    this.isRandColor =false,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {

    // 复制和分享内容
    var copytext ="";
    var shareText="";
    // 定义回调函数 用于修改分享内容和复制内容。
    void updateShareText({String newShareText="",String newcopyText=""}) {
      if(newShareText!=""){
        shareText = newShareText;
      }
      if(newcopyText!=""){
        copytext = newcopyText;
      }
    }
    Widget subtitle=SizedBox();
    var cachekey=logic.cachekey;
    if(cachekey.indexOf("rand_com_spin_wheel")!=-1){
      cachekey="rand_com_spin_wheel";
    }
    // 根据cachekey显示不同内容
    switch(cachekey) {
      case "rand_com_roller":
        subtitle= ItemResultsRoller(logic: logic, state: state,numbers:numbers,onShareTextChanged: updateShareText);
      case "rand_com_spin_wheel":
        subtitle= ItemResultsSpinWheel(logic: logic, state: state,numbers:numbers,onShareTextChanged: updateShareText);
      case "rand_com_color":
        subtitle= ItemResultsColor(logic: logic, state: state,numbers:numbers,onShareTextChanged: updateShareText);
      default:
        subtitle= ItemResultsCom(logic: logic, state: state,numbers:numbers ,onShareTextChanged: updateShareText);
    }

    shareText=globalData.appName+"\n\n"+state.shareText.value+"\n\n"+"App下载地址".tr+"\n${globalData.appInfo.ch?.updateUrl}";

    return Card(
      elevation: 3, // 设置卡片阴影
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(12), // 设置卡片圆角
        side: BorderSide(
          color: Colors.grey.withOpacity(0.3), // 设置边框颜色
          width: 1, // 设置边框宽度
        ),
      ),
      color: bgColor, // 设置卡片背景颜色
     // margin: const EdgeInsets.symmetric(vertical: 8, horizontal: 16), // 设置卡片外边距
      child: ListTile(
        title: Row(
          children: [
            Expanded(
              child: Text(
                numbers["time"],
                style:  TextStyle(
                  color: textColor,
                  fontSize: 12,

                ),
              ),
            ),
            IconButton(
              icon:  Icon(Icons.copy,color: textColor,size: 20,),
              onPressed: () {
                // 实现复制随机数到剪贴板的功能
                Clipboard.setData(ClipboardData(text: copytext));
                ScaffoldMessenger.of(context).showSnackBar(
                   SnackBar(content: Text("已复制到剪贴板".tr)),
                );
              },
            ),
            IconButton(
              icon:  Icon(Icons.share,color: textColor,size: 20,),
              onPressed: () async {
               commonFuc.shareImageWithText(text:state.randComArgs.value.title+"\n"+shareText.tr, title: globalData.appName,url: globalData.appInfo.ch?.updateUrl,filePath: "assets/images/logo100x100.png");
              },
            ),
          ],
        ),
        subtitle: subtitle,
        isThreeLine: true,
        //contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12), // 设置内容内边距
      ),
    );
  }
}
